import re


def search_pi(pi):
    pattern = '(19[8|9][0-9]|2000)(0[1-9]|1[0-2])([0-2][0-9]|30)'  # 时间有限，先写这么多吧。。。
    r = re.finditer(pattern, pi)
    min_dist = 10000
    min_start = 0
    for i in r:
        start = i.span()[0]
        end = i.span()[1]
        dist = abs(int(pi[start:end]) - 19830814)
        if dist < min_dist:
            min_dist = dist
            min_start = start
    return min_start


if __name__ == '__main__':
    with open('pi.txt', 'r') as f:
        pi = f.read()
        start = search_pi(pi)
        print('离自己生日最近的一个日期：%s, 从位置%d开始' % (pi[start:start + 8], start))
